package com.eshop.modules.user.service.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eshop.common.mapper.CoreMapper;
import com.eshop.modules.user.domain.UserBill;
import com.eshop.modules.user.service.dto.BillOrderRecordDto;
import com.eshop.modules.user.service.dto.MUserBillDto;
import com.eshop.modules.user.service.dto.YxUserBillDto;
import com.eshop.modules.user.vo.BillVo;
import com.eshop.modules.user.vo.SignVo;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Repository
public interface UserBillMapper extends CoreMapper<UserBill> {
    @Select({"<script> select b.title,b.pm,b.category,b.type,b.number,b.create_time ,u.nickname from user_bill b left join eshop_user u on u.uid=b.uid  where 1=1  <if test =\"category !=''\">and b.category=#{category}</if> <if test =\"type !=''\">and b.type=#{type}</if> <if test =\"title !=''\">and b.title=#{title}</if> <if test =\"pm !=null\">and b.pm=#{pm}</if> <if test =\"date !=null\">and b.create_time &gt;= STR_TO_DATE(#{date},'%Y-%m-%d %H:%i:%s')</if> <if test =\"date1 !=null\">and b.create_time &lt;=STR_TO_DATE(#{date1},'%Y-%m-%d %H:%i:%s')</if> <if test =\"nickname !=''\">and u.nickname LIKE CONCAT('%',#{nickname},'%')</if> order by b.create_time desc </script> "})
    List<YxUserBillDto> findAllByQueryCriteria(@Param("category") String str, @Param("type") String str2, @Param("nickname") String str3, @Param("pm") Integer num, @Param("date") String str4, @Param("date1") String str5, @Param("title") String str6);

    @Select({"SELECT DATE_FORMAT(create_time,'%Y-%m') as time,group_concat(id SEPARATOR ',') ids  FROM user_bill ${ew.customSqlSegment}"})
    List<BillVo> getBillList(@Param("ew") Wrapper<UserBill> wrapper, Page page);

    @Select({"SELECT DATE_FORMAT(create_time,'%Y-%m') as time  FROM user_bill ${ew.customSqlSegment}"})
    List<String> getBillOrderList(@Param("ew") Wrapper<UserBill> wrapper, Page page);

    @Select({"SELECT o.order_id as orderId,DATE_FORMAT(b.create_time, '%Y-%m-%d %H:%i') as time,b.number,u.avatar,u.nickname FROM user_bill b INNER JOIN store_order o ON o.id=b.link_id RIGHT JOIN eshop_user u ON u.uid=o.uid WHERE b.uid = #{uid} AND ( DATE_FORMAT(b.create_time, '%Y-%m')= #{time} ) AND b.category = 'now_money' AND b.type = 'brokerage' ORDER BY time DESC"})
    List<BillOrderRecordDto> getBillOrderRList(@Param("time") String str, @Param("uid") Long l);

    @Select({"SELECT DATE_FORMAT(a.create_time,'%Y-%m-%d') as addTime,a.title,a.number FROM user_bill a INNER JOIN eshop_user u ON u.uid=a.uid WHERE a.category = 'integral' AND a.type = 'sign' AND a.status = 1 AND a.uid = #{uid} ORDER BY a.create_time DESC"})
    List<SignVo> getSignList(@Param("uid") Long l, Page page);

    @Select({"SELECT DATE_FORMAT(create_time,'%Y-%m-%d %H:%i') as add_time,title,number,pm  FROM user_bill ${ew.customSqlSegment}"})
    List<MUserBillDto> getUserBillList(@Param("ew") Wrapper<UserBill> wrapper);

    @Select({"select IFNULL(sum(number),0) from user_bill where status=1 and type='sign' and pm=1 and category='integral' and uid=#{uid}"})
    double sumIntegral(@Param("uid") Long l);

    @Select({"select IFNULL(sum(number),0) from user_bill where status=1 and type='brokerage' and pm=1 and category='now_money' and uid=#{uid}"})
    double sumPrice(@Param("uid") int i);

    @Select({"select IFNULL(sum(number),0) from user_bill where status=1 and type='recharge' and pm=1 and category='now_money' and uid=#{uid}"})
    double sumRechargePrice(@Param("uid") Long l);

    @Select({"select IFNULL(sum(number),0) from user_bill where status=1 and type='brokerage' and pm=1 and category='now_money' and uid=#{uid} and TO_DAYS(NOW()) - TO_DAYS(create_time) <= 1"})
    double sumYesterdayPrice(@Param("uid") Long l);
}
